package com.hw.paltfrom.service.weight;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwBaseAreaMapper;
import com.hw.paltfrom.mapper.HwDataOffsiteHistoryMapper;
import com.hw.paltfrom.pojo.HwBaseArea;
import com.hw.paltfrom.pojo.HwDataOffsiteHistory;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.MapRemoveNullUtil;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.DataOffsiteVO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 非现场检测数据管理 梁天 2019/11/06
 *
 **/
@Service
public class DataOffSiteHistoryServer {

	@Autowired
	private HwDataOffsiteHistoryMapper hwDataOffsiteHistoryMapper;
	@Autowired
	private HwBaseAreaMapper hwBaseAreaMapper;

	/**
	 * 用户遍历+分页+模糊查询
	 *
	 * @param vo
	 * @return
	 */
	public Map<String, Object> queryAllList(HashMap<String, Object> vo) {

		int pageName = Integer.parseInt(vo.get("pageNum").toString());
		int pageSize = Integer.parseInt(vo.get("pageSize").toString());
		PageHelper.startPage(pageName, pageSize);

		String cph = null;
		if (vo.get("cph") != null && !"".equals(vo.get("cph"))) {
			cph = vo.get("cph").toString();
		}

		String cxl = null;
		if (vo.get("cxl") != null && !"".equals(vo.get("cxl"))) {
			cxl = vo.get("cxl").toString();
		}

		String symbol = null;
		if (vo.get("symbol") != null && !"".equals(vo.get("symbol"))) {
			symbol = vo.get("symbol").toString();
		}

		vo.remove("cxl");
		vo.remove("symbol");
		vo.remove("cph");
		vo.remove("pageNum");
		vo.remove("pageSize");
		MapRemoveNullUtil.removeNullEntry(vo);

		Wrapper<HwDataOffsiteHistory> namewrapper = new EntityWrapper<>();

		if (vo.get("F_AreaCode") != null) {// 查询是否拥有区域编码
			Object areaCode = vo.get("F_AreaCode");
			// 有区域编码
			Wrapper<HwBaseArea> namewrapper2 = new EntityWrapper<>();
			namewrapper2.eq("F_AreaCode", areaCode).and().eq("F_ParentId", "0");// 查询是否为市
			List<HwBaseArea> hwBaseUserList = hwBaseAreaMapper.selectList(namewrapper2);
			if (hwBaseUserList.isEmpty()) {
				// 结果为空,区县查询
				namewrapper.allEq(vo);
				if (cph != null) {
					namewrapper.and().like("CPH", cph);
				}
				namewrapper.orderBy("CreateTime",false);
			} else {
				vo.remove("F_AreaCode");
				// 非空,全市查询
				namewrapper.allEq(vo).and().like("CPH", cph).orderBy("CreateTime",false);
			}
		} else {
			// 无区域编码,进行站点查询
			namewrapper.allEq(vo);
			if (cph != null) {
				namewrapper.and().like("CPH", cph);
			}
			namewrapper.orderBy("CreateTime",false);
		}

		if (StringUtils.isNotBlank(symbol) && StringUtils.isNotBlank(cxl)) {
			switch (symbol) {
			case "0":
				namewrapper.eq("CXL", cxl);
				break;
			case "1":
				namewrapper.gt("CXL", cxl);
				break;
			case "2":
				namewrapper.lt("CXL", cxl);
				break;
			case "3":
				namewrapper.ge("CXL", cxl);
				break;
			case "4":
				namewrapper.le("CXL", cxl);
				break;
			}

		}

		List<HwDataOffsiteHistory> hwBaseUserList = hwDataOffsiteHistoryMapper.selectList(namewrapper);
		PageInfo<HwDataOffsiteHistory> info = new PageInfo<>(hwBaseUserList);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
	}

	public Map<String, Object> insert(DataOffsiteVO vo) {

		HwDataOffsiteHistory hwDataOffsiteHistory = new HwDataOffsiteHistory();
		hwDataOffsiteHistory.setfId(RandomNumberUtil.getNumber());
		hwDataOffsiteHistory.setfOffsitecode(vo.getfOffsitecode());
		hwDataOffsiteHistory.setfOffsitename(vo.getfOffsitename());
		hwDataOffsiteHistory.setfAreacode(vo.getfAreacode());
		hwDataOffsiteHistory.setCph(vo.getCph());
		hwDataOffsiteHistory.setCpys(vo.getCpys());
		hwDataOffsiteHistory.setZz(vo.getZz());
		hwDataOffsiteHistory.setZs(vo.getZs());
		hwDataOffsiteHistory.setCs(vo.getCs());
		hwDataOffsiteHistory.setXz(vo.getXz());
		hwDataOffsiteHistory.setCxl(vo.getCxl());
		hwDataOffsiteHistory.setCptx(vo.getCptx());
		hwDataOffsiteHistory.setJcsj(new Timestamp(System.currentTimeMillis()));
		hwDataOffsiteHistory.setZx(vo.getZx());
		hwDataOffsiteHistory.setCd(vo.getCd());
		hwDataOffsiteHistory.setCreateTime(new Timestamp(System.currentTimeMillis()));
		Integer insert = hwDataOffsiteHistoryMapper.insert(hwDataOffsiteHistory);
		if (insert != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}
}
